package com.qin.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qin.mall.domain.OmsOrderOperateHistory;
import com.qin.mall.dto.*;
import com.qin.mall.dao.OmsOrderDao;
import com.qin.mall.domain.OmsOrder;

import com.qin.mall.mapper.OmsOrderOperateHistoryMapper;
import com.qin.mall.service.OmsOrderService;
import com.qin.mall.mapper.OmsOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author 覃剑
* @description 针对表【oms_order(订单表)】的数据库操作Service实现
* @createDate 2022-11-09 18:35:47
*/
@Service
public class OmsOrderServiceImpl extends ServiceImpl<OmsOrderMapper, OmsOrder>
    implements OmsOrderService{
    
    @Autowired
    private OmsOrderMapper orderMapper;
    @Autowired
    private OmsOrderDao orderDao;
    @Autowired
    private OmsOrderOperateHistoryMapper orderOperateHistoryMapper;

    /**
     * 分页查询订单查询
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<OmsOrder> list(Integer pageNum, Integer pageSize) {
        Page<OmsOrder> page = new Page<>(pageNum, pageSize);
        Page<OmsOrder> list = orderMapper.selectPage(page, null);
//        List<OmsOrder> orders = list.getRecords();
        return list;
    }

    /**
     * 分页条件查询订单
     * @param queryParam
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Page<OmsOrder> list(OmsOrderQueryParam queryParam, Integer pageNum, Integer pageSize) {
        Page<OmsOrder> page = new Page<>(pageNum, pageSize);
        QueryWrapper<OmsOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("delete_status",0);
        //下面是判读各个属性是否为空，然后加入构造条件
        if (queryParam.getOrderSn() != null&& !Objects.equals(queryParam.getOrderSn(), "")) {
            wrapper.eq("order_sn",queryParam.getOrderSn());
        }
        if (queryParam.getStatus()!=null){
            wrapper.eq("status",queryParam.getStatus());
        }
        if (queryParam.getSourceType()!=null){
            wrapper.eq("source_type",queryParam.getSourceType());
        }
        if (queryParam.getOrderType()!=null){
            wrapper.eq("order_type",queryParam.getOrderType());
        }
        if (queryParam.getCreateTime()!=null
                && !Objects.equals(queryParam.getCreateTime(), "")){
            wrapper.like("create_time","%"+queryParam.getCreateTime()+"%");
        }
        if (queryParam.getReceiverKeyword()!=null
                && !Objects.equals(queryParam.getReceiverKeyword(), "")){
            wrapper.and(i->i.like("receiver_name",
                            queryParam.getReceiverKeyword()+"%")
                    .or().like("receiver_phone",queryParam.getReceiverKeyword()));
        }
        Page<OmsOrder> list = orderMapper.selectPage(page, wrapper);
//        List<OmsOrder> orders = list.getRecords();
        return list;
    }


    /**
     * 批量发货
     * @param deliveryParamList
     * @return
     */
    @Override
    public int delivery(List<OmsOrderDeliveryParam> deliveryParamList) {
        //批量发货
        int count = orderDao.delivery(deliveryParamList);
        //添加操作记录
        List<OmsOrderOperateHistory> operateHistoryList = deliveryParamList.stream()
                .map(omsOrderDeliveryParam -> {
                    OmsOrderOperateHistory history = new OmsOrderOperateHistory();
                    history.setOrderId(omsOrderDeliveryParam.getOrderId());
                    history.setCreateTime(new Date());
                    history.setOperateMan("后台管理员");
                    history.setOrderStatus(2);
                    history.setNote("完成发货");
                    return history;
                }).collect(Collectors.toList());
        if(count==deliveryParamList.size()){
        for (OmsOrderOperateHistory history :operateHistoryList ) {
            int save = orderOperateHistoryMapper.insert(history);
            save++;
        }}
        return count;
    }

    /**
     * 批量关闭订单
     * @param ids
     * @param note
     * @return
     */
    @Override
    public int close(List<Long> ids, String note) {
        OmsOrder record = new OmsOrder();
        record.setStatus(4);
        record.setNote(note);
        QueryWrapper<OmsOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("delete_status",0)
                .in("id",ids);
        int update = orderMapper.update(record, wrapper);
        List<OmsOrderOperateHistory> historyList = ids.stream().map(orderId -> {
            OmsOrderOperateHistory history = new OmsOrderOperateHistory();
            history.setOrderId(orderId);
            history.setCreateTime(new Date());
            history.setOperateMan("后台管理员");
            history.setOrderStatus(4);
            history.setNote("订单关闭:"+note);
            return history;
        }).collect(Collectors.toList());
        if (update>0){
            for (OmsOrderOperateHistory history :historyList ) {
                int save = orderOperateHistoryMapper.insert(history);
                save++;
            }
        }
        return update;
    }

    /**
     * 批量删除订单
     * @param ids
     * @return
     */
    @Override
    public int delete(List<Long> ids) {
        OmsOrder record = new OmsOrder();
        record.setDeleteStatus(1);
        QueryWrapper<OmsOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("delete_status",0)
                .in("id",ids);
        int count = orderMapper.update(record, wrapper);
        return count;
    }

    /**
     * 获取指定订单详情
     * @param id
     * @return
     */
    @Override
    public OmsOrderDetail detail(Long id) {
        OmsOrderDetail order = orderDao.getDetail(id);
        return order;
    }

    /**
     * 修改订单收货人信息
     * @param receiverInfoParam
     * @return
     */
    @Override
    public int updateReceiverInfo(OmsReceiverInfoParam receiverInfoParam) {
        OmsOrder order = new OmsOrder();
        order.setId(receiverInfoParam.getOrderId());
        order.setReceiverName(receiverInfoParam.getReceiverName());
        order.setReceiverPhone(receiverInfoParam.getReceiverPhone());
        order.setReceiverPostCode(receiverInfoParam.getReceiverPostCode());
        order.setReceiverDetailAddress(receiverInfoParam.getReceiverDetailAddress());
        order.setReceiverProvince(receiverInfoParam.getReceiverProvince());
        order.setReceiverCity(receiverInfoParam.getReceiverCity());
        order.setReceiverRegion(receiverInfoParam.getReceiverRegion());
        order.setModifyTime(new Date());
        int update = orderMapper.updateById(order);
        //插入操作记录
        OmsOrderOperateHistory history = new OmsOrderOperateHistory();
        history.setOrderId(receiverInfoParam.getOrderId());
        history.setCreateTime(new Date());
        history.setOperateMan("后台管理员");
        history.setOrderStatus(receiverInfoParam.getStatus());
        history.setNote("修改收货人信息");
        int insert = orderOperateHistoryMapper.insert(history);
        return update;
    }

    /**
     * 修改订单费用信息
     * @param moneyInfoParam
     * @return
     */
    @Override
    public int updateMoneyInfo(OmsMoneyInfoParam moneyInfoParam) {
        OmsOrder order = new OmsOrder();
        order.setId(moneyInfoParam.getOrderId());
        order.setFreightAmount(moneyInfoParam.getFreightAmount());
        order.setDiscountAmount(moneyInfoParam.getDiscountAmount());
        order.setModifyTime(new Date());
        int update = orderMapper.updateById(order);
        //插入操作记录
        OmsOrderOperateHistory history = new OmsOrderOperateHistory();
        history.setOrderId(moneyInfoParam.getOrderId());
        history.setCreateTime(new Date());
        history.setOperateMan("后台管理员");
        history.setOrderStatus(moneyInfoParam.getStatus());
        history.setNote("修改费用信息");
        int insert = orderOperateHistoryMapper.insert(history);
        return update;
    }

    /**
     * 修改订单备注
     * @param id
     * @param note
     * @param status
     * @return
     */
    @Override
    public int updateNote(Long id, String note, Integer status) {
        OmsOrder order = new OmsOrder();
        order.setId(id);
        order.setNote(note);
        order.setModifyTime(new Date());
        int update = orderMapper.updateById(order);
        //插入记录表
        OmsOrderOperateHistory history = new OmsOrderOperateHistory();
        history.setOrderId(id);
        history.setCreateTime(new Date());
        history.setOperateMan("后台管理员");
        history.setOrderStatus(status);
        history.setNote("修改备注信息："+note);
        int insert = orderOperateHistoryMapper.insert(history);
        return update;
    }
}




